# -*- coding: utf-8 -*-
import os
from apscheduler.schedulers.background import BackgroundScheduler
import time
from scrapy.utils.log import configure_logging
from multiprocessing import Process, Queue
from twisted.internet import reactor
import scrapy.crawler as crawler
import logging
from scrapy.utils.project import get_project_settings
import redis

redis_db = redis.Redis(host='127.0.0.1', port=6379, db=0) #连接redis，相当于MySQL的conn

def f(q):

    try:
        configure_logging({'LOG_FORMAT': '%(levelname)s: %(message)s'})
        runner = crawler.CrawlerRunner(get_project_settings())
        deferred = runner.crawl('mpo_baidu')#启动程序
        deferred.addBoth(lambda _: reactor.stop())
        reactor.run()
        q.put(None)
    except Exception as e:
        q.put(e)



def job():
    print ('开始..')
    global_switch = redis_db.hget('crawl2018:config', 'global_switch')
    if global_switch == 'true':
        q = Queue()
        p = Process(target=f, args=(q,))
        p.start()
        result = q.get()
        p.join()

        if result is not None:
            raise result

        if result is not None:
            raise result
    else:
        print('暂时关闭');


if __name__ == '__main__':

    log = logging.getLogger('apscheduler.executors.default')
    log.setLevel(logging.INFO)  # DEBUG

    fmt = logging.Formatter('%(levelname)s:%(name)s:%(message)s')
    h = logging.StreamHandler()
    h.setFormatter(fmt)
    log.addHandler(h)
    scheduler = BackgroundScheduler()
    scheduler.add_job(job, 'interval', seconds=3) #间隔3秒钟执行一次
    # scheduler.add_job(job, 'cron', second='*/3') #间隔3秒钟执行一次
    scheduler.start()    #这里的调度任务是独立的一个线程
    print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))

    try:
        # This is here to simulate application activity (which keeps the main thread alive).
        while True:
            time.sleep(2)    #其他任务是独立的线程执行
            print('休息2秒!')
    except (KeyboardInterrupt, SystemExit):
        # Not strictly necessary if daemonic mode is enabled but should be done if possible
        scheduler.shutdown()
        print('Exit The Job!')
